package com.ysd.Controller;

import java.io.UnsupportedEncodingException;
import java.util.Date;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ysd.entity.UserInfo;
import com.ysd.util.RedisUtil;
import com.ysd.util.ResultInfoUtil1;
import com.ysd.util.TokenUtil;
@CrossOrigin
@RestController
@RequestMapping("/index")
public class LoginController {
	
	@Autowired
    private RedisUtil redisUtil;


	@GetMapping("/login")
	public ResultInfoUtil1 login(String uname, String passcheck) {
			System.out.println("222");
		// 获取subject操作主体
		Subject subject = SecurityUtils.getSubject();
		
		System.out.println(1);

		// 设置登录的用户名密码
		UsernamePasswordToken token = new UsernamePasswordToken(uname, passcheck);
		String tk = null;
		try {
			subject.login(token);
			//获取当前登录的用户
			UserInfo user = (UserInfo) subject.getPrincipal();
			//自定义一个用户，用于生产token
			UserInfo u = new UserInfo();
			
			u.setUname(user.getUname());
			
			try {
				tk = TokenUtil.sign(u, 1 * 60 * 60 * 100);
				System.out.println("tk123"+tk);
				//redisUtil.set(u.getUname(), tk, 1 * 60 * 60 * 100);
				
			} catch (UnsupportedEncodingException | JsonProcessingException e) {
				e.printStackTrace();
			}

			return new ResultInfoUtil1(0, tk, subject.getPrincipal());

		} catch (UnknownAccountException e) {
			return new ResultInfoUtil1(-1, "用户名错误！", null);
		} catch (IncorrectCredentialsException e) {
			return new ResultInfoUtil1(-1, "密码错误！", null);
			
		}
	}

	@RequestMapping("/test")
	public ResultInfoUtil1 test() {

		return new ResultInfoUtil1(0, "我谁都可以访问", null);
	}

	@RequestMapping("/require_auth")
	public ResultInfoUtil1 requireAuth() {

		return new ResultInfoUtil1(0, "必须登录才可以访问", null);
	}

	@RequestMapping("/require_role")
	@RequiresRoles(value = "admin") // 这个接口必须是admin角色才能访问
	public ResultInfoUtil1 requireRole() {

		return new ResultInfoUtil1(0, "必须有admin角色才可以访问", null);
	}

	@PostMapping("/require_permission")
	@RequiresPermissions(value = "user:add")
	public ResultInfoUtil1 requirePermission() {

		return new ResultInfoUtil1(0, "必须有user:add权限才可以访问", null);
	}

	@RequestMapping("/logout")
	public ResultInfoUtil1 logout() {
		// 获取subject操作主体
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.logout();
			return new ResultInfoUtil1(0, "退出成功！", null);
		} catch (Exception e) {
			return new ResultInfoUtil1(-1, "退出失败！", null);
		}

	}

	@RequestMapping("/golog")
	public ResultInfoUtil1 golog() {

		return new ResultInfoUtil1(402, "去登录", null);
	}

	@RequestMapping("/unauthorized")
	public ResultInfoUtil1 unauthorized() {

		return new ResultInfoUtil1(401, "没有权限", null);
	}
	
	@RequestMapping("/yanzheng")
	public String yanzheng() {
		System.out.println("执行了一次验证");
		return "ok";
	}

}
